﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebCam.Data.WebCamWorker;
using System.Drawing;
using WebcamCore;
using System.Drawing.Imaging;

namespace WebCam.Logic.Detectors
{
    public class SimpleControllerEventArgs : WorkerComletedEventArgs
    {
        List<int> selected;
        public SimpleControllerEventArgs(Bitmap bmp, List<int> selected)
            : base(bmp)
        {
            this.selected = selected;
        }
        public List<int> Selected { get { return selected; } }
    }

    public class SimpleControllerWorker : IWorker
    {
        List<ControlArea> areasForControl;
        public SimpleControllerWorker(List<ControlArea> areasForControl)
        {
            this.areasForControl = areasForControl;
        }

        private int delta = 50;

        public void ProcessBitmap(System.Drawing.Bitmap image)
        {
            Bitmap tmp = (Bitmap)image.Clone();
            List<int> selected = new List<int>();

            foreach (var area in areasForControl)
            {
                BitmapData imageData = image.LockBits(area.Rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                BitmapData tempData = tmp.LockBits(area.Rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);               

                int count = 0;
                unsafe
                {
                    byte* pImage = (byte*)imageData.Scan0.ToPointer();
                    byte* pTemp = (byte*)tempData.Scan0.ToPointer();
                    

                    for (int j = 0; j < area.Rect.Height; j++)
                    {
                        for (int i = 0; i < area.Rect.Width; i++)
                        {
                            byte b = *(pImage);
                            byte g = *(pImage + 1);
                            byte r = *(pImage + 2);

                            if (r < delta && b < delta && g < delta)
                            {
                                *(pTemp) = 0;
                                *(pTemp + 1) = 0;
                                *(pTemp + 2) = 255;
                                count++;
                            }
                            pImage += 3;
                            pTemp += 3;
                        }
                        pTemp += (image.Width - area.Rect.Width) * 3;
                        pImage += (image.Width - area.Rect.Width) * 3;
                    }
                }
                image.UnlockBits(imageData);
                tmp.UnlockBits(tempData);

                if (count > area.Rect.Width * area.Rect.Height / 2)
                {
                    selected.Add(area.ID);
                }
            }

            if (OnWorkerComplete != null)
            {
                OnWorkerComplete(this, new SimpleControllerEventArgs(tmp, selected));
            }
        }

        public event WorkerComplete OnWorkerComplete;
    }
}
